package org.linlinjava.litemall.wx.web;

import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.db.service.LitemallRentOrderService;
import org.linlinjava.litemall.wx.annotation.LoginUser;
import org.linlinjava.litemall.wx.dto.RentOrderDto;
import org.linlinjava.litemall.db.dto.ReturnBookDto;
import org.linlinjava.litemall.wx.service.WxRentOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

/**
 * @author Administrator
 */
@RestController
@RequestMapping("/wx/rentOrder")
public class WxRentOrderController {
    @Autowired
    private WxRentOrderService wxRentOrderService;
    @Autowired
    private LitemallRentOrderService rentOrderService;

    /**
     * 提交租书订单
     *
     * @param userId
     * @param rentOrderDto
     * @return
     */
    @PostMapping("/submit")
    public Object submitRentOrder(@LoginUser Integer userId, @RequestBody RentOrderDto rentOrderDto) {
        return wxRentOrderService.submitRentOrder(userId, rentOrderDto);
    }

    /**
     * 管理员下单
     * @param rentOrderDto
     * @return
     */
    @PostMapping("/adminSubmit")
    public Object adminSubmitRentOrder( @RequestBody RentOrderDto rentOrderDto) {
        return wxRentOrderService.submitRentOrder(rentOrderDto);
    }

    /**
     * 查询租借订单list
     *
     * @param userId
     * @param status
     * @param page
     * @param limit
     * @param sort
     * @param order
     * @return
     */
    @GetMapping("/list")
    public Object listRentOrder(@LoginUser Integer userId,
                                @RequestParam Integer status,
                                @RequestParam(required = false, defaultValue = "1") Integer page,
                                @RequestParam(required = false, defaultValue = "10") Integer limit,
                                @RequestParam(required = false, defaultValue = "add_time") String sort,
                                @RequestParam(required = false, defaultValue = "desc") String order) {
        return wxRentOrderService.queryList(userId, status, page, limit, sort, order);
    }

    @GetMapping("/adminList")
    public Object adminListRentOrder(@RequestParam Integer userId,
                                @RequestParam(required = false ,defaultValue ="100") Integer status,
                                @RequestParam(required = false, defaultValue = "1") Integer page,
                                @RequestParam(required = false, defaultValue = "10") Integer limit,
                                @RequestParam(required = false, defaultValue = "add_time") String sort,
                                @RequestParam(required = false, defaultValue = "desc") String order) {
        return wxRentOrderService.queryList(userId, status, page, limit, sort, order);
    }

    /**
     * 查询订单详情
     * @param rentOrderId
     * @return
     */
    @GetMapping("/detail")
    public Object queryDetail(@RequestParam Integer rentOrderId) {
        return ResponseUtil.ok(rentOrderService.queryRentOrderDetail(rentOrderId));
    }

    /**
     * 确认租书订单信息
     *
     * @param userId
     * @param rentOrderDto
     * @return
     */
    @PostMapping("/checkOut")
    public Object checkOut(@LoginUser Integer userId, @RequestBody RentOrderDto rentOrderDto) {
        if (userId == null) {
            return ResponseUtil.unlogin();
        }
        return wxRentOrderService.checkOut(userId, rentOrderDto);
    }

    /**
     * 用户确认收货
     *
     * @param userId
     * @param rentOrderId
     * @return
     */
    @PostMapping("/confirm")
    public Object confirm(@LoginUser Integer userId, @RequestBody Integer rentOrderId) {
        return wxRentOrderService.confirm(userId, rentOrderId);
    }

    /**
     * 用户取消订单
     *
     * @param userId
     * @param rentOrderId
     * @return
     */
    @PostMapping("/cancel")
    public Object cancel(@LoginUser Integer userId, @RequestBody Integer rentOrderId) {
        return wxRentOrderService.cancel(userId, rentOrderId);
    }

    /**
     * 逻辑删除订单
     * @param rentOrderId
     * @return
     */
    @PostMapping("logical/delete")
    public Object logicalDelete(@RequestBody Integer rentOrderId) {
        return wxRentOrderService.logicalDelete(rentOrderId);
    }
    /**
     * 导出Excel
     *
     * @param type
     * @param response
     * @return
     */
    @PostMapping("/exportExcel")
    public Object exportExcel(@RequestBody String type, HttpServletResponse response) {
        return wxRentOrderService.exportExcel(type, response);
    }

    /**
     * 用户点击还书
     * @param userId
     * @param returnBookDto
     * @return
     */
    @PostMapping("/returnBooks")
    public Object returnBooks(@LoginUser Integer userId,@RequestBody ReturnBookDto returnBookDto) {
        return wxRentOrderService.returnBooks(userId,returnBookDto);
    }

    /**
     * 获取租书订单物流轨迹
     * @param rentOrderId
     * @return
     */
    @GetMapping("/getExpressTrace")
    public Object getExpressTrace(@RequestParam Integer rentOrderId) {
        return wxRentOrderService.getExpressTrace(rentOrderId);
    }


}
